package cn.cdqf.coupon.util;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class IdUtils {

    private final String string[] = {"a","b","c","1","2","3","d","e","f","g","h","4","5","6","7","8","9"};
    //雪花算法
    @Async("getAsyncExecutor")
    //分库分表,将表分为不同的库
    public Future<String> getId(String id){
        StringBuilder sb = new StringBuilder(id);
        StringBuilder finalId = new StringBuilder("");

        //去前三位和后三位作为前六位
        String substring = sb.substring(0, 3);
        String substring1 = sb.substring(sb.length() - 4);

        //前六位 第七位+第八位+第九位/3作为选库的一句
        Random random = new Random();
        int i = random.nextInt(10);
        int j = random.nextInt(10);
        int k = random.nextInt(10);
        //通过流转成集合
        List<String> li = Stream.of(string).collect(Collectors.toList());
        //打乱
        Collections.shuffle(li);
        //也可以在join里面加,
        String collect = li.stream().collect(Collectors.joining());
        finalId.append(substring).append(substring1).append(i).append(j).append(k).append(collect);
        return new AsyncResult(finalId.toString());
    }

    public String getIdNoAsync(String id){
        StringBuilder sb = new StringBuilder(id);
        StringBuilder finalId = new StringBuilder("");

        //去前三位和后三位作为前六位
        String substring = sb.substring(0, 3);
        String substring1 = sb.substring(sb.length() - 4);

        //前六位 第七位+第八位+第九位/3作为选库的一句
        Random random = new Random();
        int i = random.nextInt(10);
        int j = random.nextInt(10);
        int k = random.nextInt(10);
        //通过流转成集合
        List<String> li = Stream.of(string).collect(Collectors.toList());
        //打乱
        Collections.shuffle(li);
        //也可以在join里面加,
        String collect = li.stream().collect(Collectors.joining());
        finalId.append(substring).append(substring1).append(i).append(j).append(k).append(collect);
        return finalId.toString();
    }
}
